/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.persistence.ui.handler;

import java.io.File;

import net.jonbuck.tassoo.persistence.dao.TassooDao;
import net.jonbuck.tassoo.persistence.dao.TassooDaoImpl;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;

/**
 * 
 * 
 * @since 1.0.0
 */
public class OpenTaskStoreHandler extends AbstractHandler {

	TassooDao tassooDao = TassooDaoImpl.getInstance();

	/**
	 * 
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {

		// We need to check to see if the user already has a task store open and
		// if they do then prompt them to save the contents before opening the
		// new task store.
		if (tassooDao.isLoaded()) {
			if (MessageDialog.openQuestion(Display.getDefault().getActiveShell(), "Save Current Task Store",
					"Would you like to save the current task store before opening a new one?")) {
				tassooDao.closeTaskStore(true);
			} else {
				tassooDao.closeTaskStore(false);
			}
		}

		FileDialog fileDialog = new FileDialog(Display.getDefault().getActiveShell());
		fileDialog.setFilterExtensions(new String[] { "*.tassoo" });
		if (StringUtils.isNotBlank(fileDialog.open())) {
			tassooDao.loadTaskStore(fileDialog.getFilterPath().concat(File.separator).concat(fileDialog.getFileName()));
		}
		return null;
	}

}
